package atguigu2.readwrite;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @AUTHOR LYF
 * @DATE 2022/2/4
 * @VERSION 1.0
 * @DESC
 * 读写锁升级，降级
 *
 * 写锁可以降为读锁
 *
 * 而读不能升级为写锁
 *
 */
public class ReadWriteDemo {

    public static void main(String[] args) {
        ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
        ReentrantReadWriteLock.ReadLock readLock = readWriteLock.readLock();//读锁
        ReentrantReadWriteLock.WriteLock writeLock = readWriteLock.writeLock();

//        // 1. 获取写锁
//        writeLock.atguigu2.lock();
//        System.out.println("写操作...");
//        // 2. 获取读锁
//        readLock.atguigu2.lock();
//        System.out.println("进行读...");

//        写操作...
//        进行读...

        // 1. 获取读锁
        readLock.lock();
        System.out.println("进行读...");
        //readLock.unlock();

        // 2. 获取写锁
        writeLock.lock();
        System.out.println("写操作...");

//        进行读... ;先进读，再写只能等到读锁释放才能读



    }
}
